<?php
/*
	代理中心
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
//$this->model('member')->set_parent(90,1);
$this->model('member')->min_agent_level();
$this->model('member')->upbao_agent();
$this->session('footernav','');
if($_RQ['op']=='main'){
	$member=$this->get('member',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']));
	$return['setting']=$this->plugin_setting('page_setting');
	$diytitle=array('tagent','tshop','tmyshop','tcenter','tbecome','balance_button','tcommission','tcommission1','commission_total','commission_1','balance_total','balance_1','commission_1_1','commission_0','balance_2','balance_3','balance_pay','tcommission_detail','torder','tdown','tmydown','area_title','commission_area');
	foreach($diytitle as $v){
		$return['setting'][$v]=$return['setting'][$v.'2']?:$return['setting'][$v];
	}
	if($return['pluginset']['levefromid']){
		$level=$this->getcolumn('level',array('id'=>$return['pluginset']['levefromid']),'level');
		$parameter=array(
				'params'=>array('uniacid'=>$_SESSION['uniacid'],'level <'=>$level),
				'orderby'=>'level desc',
		);
		$levels=$this->getall('level',$parameter['params'],'','',$parameter['orderby']);
		if($member['level_id']==intval($levels[0]['id'])){
			$return['showlevefrom']=true;
		}
	}
	$return['xieyi']=$this->plugin_setting('xieyi');
	$tagent=$this->getcolumn('level',array('id'=>$return['pluginset']['levefromid']),'title');
	$tagent=$tagent?:'上级';
	$return['tagent']=$tagent;
	set_html('title',$return['setting']['title']?:'代理中心');
	$basic=model('core/member')->basic($_SESSION['uid']);
	$member=array_merge($member,$basic);
	if($member['agent_id']){
		$member['agent']=model('core/member')->basic($member['agent_id']);
	}else{
		$member['agent']=array('nickname'=>'无');
	}
	if($member['level_id']){
		$member['level']=$this->getcolumn('level',array('id'=>$member['level_id']),'title');
	}else{
		$member['level']=$return['setting']['demolevel'];
	}
	$return['ordernum']=0;
	$return['feenum'] =$this->count('commission',array('mid'=>$_SESSION['uid'],'money >'=>0));
	if($member['redfee']<0){
		$member['redfee']=0;
	}
	$member['redfee']=price($member['redfee'],'',true);
	$return['member']=$member;
	$data=array();
	//已结算佣金
	$data['balance_total']=$this->sum('balance',array('mid'=>$_SESSION['uid']),'money');
	$data['balance_num']=$this->count('balance',array('mid'=>$_SESSION['uid']));
	//待结算佣金
	$params=array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'status'=>1,'isxuni'=>0);
	/*if($this->setting['balance_day']){
		//结算天数
		$time=TIMESTAMP-intval($this->setting['balance_day'])*86400;
		$params['complete_time <']=$time;
	}*/
	$data['commission_1']=$this->sum('commission',$params,'money');
	$data['commission_1num']=$this->count('commission',$params,'id');
	$data['commission_total']=$this->sum('commission',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']),'money');
	$data['commission_num']=$this->count('commission',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']),'id');
	$data['mybuy']=pdo_getsum('jy_weishop_order',array('ispay'=>1,'mid'=>$_SESSION['uid']),'price');
    $allmember_num=$this->model('member')->getteamids($_SESSION['uid'],1,2);
	$allmember_num=count($allmember_num);
	$agent_update=array('member_num'=>$allmember_num);
	$this->update('member',$agent_update,array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']));
	$return['member']['member_num']=$allmember_num;
	$return['data']=$data;
	$htmls=array(
		array('type'=>'data','num'=>12,'one_num'=>3,'title'=>'总额/数量','datas'=>array(
			array('title'=>'总佣金','num'=>2,'data'=>array($data['commission_total'],$data['commission_num'])),
			array('title'=>'已结算','yuan'=>1,'num'=>2,'data'=>array($data['balance_total'],$data['balance_num'])),
			array('title'=>'待结算','yuan'=>1,'num'=>2,'data'=>array($data['commission_1'],$data['commission_1num'])),
			array('title'=>'团队人数','num'=>2,'data'=>array($allmember_num,$allmember_num)),
		)),
	);
	if($_SESSION['os']=='windows'){
		$return['htmls']=$htmls;
	}
	exi($return);
}
if($_RQ['op']=='apply'){
	$apply=$this->get('apply',array('mid'=>$_SESSION['uid']));
	$page_setting=$this->plugin_setting('page_setting');
	if($apply['status']==0 && $apply['id']){
		exi('您已申请，请等待审核！','error');
	}
		$post=get_postdata();
		if(!$post['agree']){
			exi('请先阅读协议！','error');
		}
		if(!$post['realname'] && $page_setting['realname']){
			exi('请输入姓名！','error');
		}
		if(!$post['mobile'] && $page_setting['mobile']){
			exi('请输入联系电话！','error');
		}
		if(!$post['wechat'] && $page_setting['wechat']){
			exi('请输入微信号！','error');
		}
		if(!preg_match("/^1[345678]{1}\d{9}$/",$post['mobile']) && $page_setting['wechat']){ 	 
			exi('请输入正确的手机号码','error');
		}
		if($post['diyformfields']){
			foreach ($post['diyformfields'] as $item) {
				if($item['ismust']==1 && empty($item['data'])){
					exi('请完善'.$item['title'].'内容后再提交','error');
				}
			}
		}
		$member_update=array(
			'realname'=>trim($post['realname']),
			'mobile'=>trim($post['mobile']),
			'diyformdata'=>ijson_encode($post['diyformfields']),
		);
		if($post['wechat']){
			$member_update['wechat']=trim($post['wechat']);
		}
		$this->model('member')->member_update($_SESSION['uid'],$member_update);	
		if($post['agent_id']){
			$agent_id=intval($post['agent_id']);
			if($agent_id){
				$this->model('member')->set_parent($_SESSION['uid'],$agent_id);
			}
		}
		$this->model('member')->apply_agent($_SESSION['uid']);
		exi('申请成功！请耐心等待管理员审核！','',custom_url('jy_weishop/index/',true));
}
?>